<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\VisitStoreRequest;
use App\Http\Requests\VisitUpdateRequest;
use App\Models\Visit;
use Auth;

class VisitController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $columns = ['时间','地点','联系人','目的','结果','备注','创建人'];
        return $this->index_model_list(Visit::class,$columns,$request,function($builder) use($request){

            $keyword = $request->get('keyword','');

            $builder->with('user');
            //自定义查询条件
            if(!empty($keyword)){
                $builder->where(function($q) use($keyword){
                    $q->where('contact_name','like','%'.$keyword.'%')
                      ->orWhere('visit_date','like','%'.$keyword.'%')
                      ->orWhere('intention','like','%'.$keyword.'%')
                      ->orWhere('address','like','%'.$keyword.'%')
                      ->orWhere('result','like','%'.$keyword.'%')
                      ->orWhere('remark','like','%'.$keyword.'%');
                });
            }
        },function($model) {
            //导出字段定义
            return [
               $model->visit_date,
               $model->address,
               $model->contact_name,
               $model->intention,
               $model->result,
               $model->remark,
               $model->user->name
           ];
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(VisitStoreRequest $request)
    {
        $data = array_null_to_empty($request->only([
            'contact_name','visit_date','address','intention',
            'result','remark'
        ]));

        $model = new Visit();
        $model->fill($data);
        $model->user_id = Auth::user()->id;
        $model->save();
        return response()->json(json_success(['id'=>$model->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Visit::with('user')->findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(VisitUpdateRequest $request, $id)
    {
        $data = array_filter($request->only([
            'contact_name','visit_date','address','intention',
            'result','remark'
        ]));
        $model = Visit::findOrFail($id);
        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = Visit::findOrFail($id);
        $model->delete();
        return response()->json(json_success());
    }
}
